//
//  SISSTenencyTenantTakeLookViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/1/5.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSTenencyTenantTakeLookViewController.h"


#import "SISSTenancyTenantTakeLookCell.h"
#import "SISSTenantTakeLookDetailViewController.h"
#import "SISSChatViewController.h"

#import "SISSNetworkingManager+TenantProgress.h"
#import "SISSNetworkingManager+Progress.h"
#import "SISSTenantTakeLookModel.h"
#import "SISSTenantProgressModel.h"
#import "SISSTenantProgressRecordInfoModel.h"

@interface SISSTenencyTenantTakeLookViewController ()<SISSTenancyTenantTakeLookCellDelegate>

/* 带看模型数组 */
@property (strong, nonatomic) NSArray<SISSTenantTakeLookModel *> *takeLookArr;

@end

@implementation SISSTenencyTenantTakeLookViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    WeakSelf
    [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeUpDown;
        [weakSelf upDownLoadData];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.isSelectMain = NO;
    [self requestRentOutTakeLookRecordData];
}

- (void)startDealButtonDidClickAction {
    [super startDealButtonDidClickAction];
    
//    SISSTenantTakeLookModel *takeLookModel = self.takeLookArr[self.selectIndexPath.row];
    
//    SISSTenancyContractViewController *vc = [[SISSTenancyContractViewController alloc] init];
//    vc.tenantProgressModel = self.tenantProgressModel;
//    vc.tenantProgressRecordInfoModel = self.tenantProgressRecordInfoModel;
//    vc.tenantTakeLookModel = takeLookModel;
//    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.takeLookArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSTenancyTenantTakeLookCell *cell = [tableView dequeueReusableCellWithIdentifier:tenancyTenantTakeLookCellReuseID forIndexPath:indexPath];
    cell.delegate = self;
    cell.tenantTakeLookModel = self.takeLookArr[indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    SISSTenantTakeLookDetailViewController *vc = [[SISSTenantTakeLookDetailViewController alloc] init];
    vc.tenantProgressModel = self.tenantProgressModel;
    //    vc.brokerState = self.takeLookArr[indexPath.row].brokerState;
    vc.takeLookModel = self.takeLookArr[indexPath.row];
    //    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- SISSTenancyRentOutTakeLookCellDelegate
- (BOOL)tenancyTenantTakeLookRecordCell:(SISSTenancyTenantTakeLookCell *)cell checkButtonIsSelected:(BOOL)isSelected {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    self.takeLookArr[self.selectIndexPath.row].isSelected = NO;
    self.takeLookArr[indexPath.row].isSelected = YES;
    self.selectIndexPath = indexPath;
    [self.tableView reloadData];
    self.isSelectMain = YES;
    
    return NO;
}

- (void)tenancyTenantTakeLookRecordChatButtonClickWithCell:(SISSTenancyTenantTakeLookCell *)cell {
    SISSChatViewController *chatVC = [[SISSChatViewController alloc] initWithConversationChatter:self.tenantProgressRecordInfoModel.imAccount conversationType:EMConversationTypeChat];
    SISSContactModel *model = [[SISSContactModel alloc] init];
    model.name = self.tenantProgressRecordInfoModel.name;
    model.accountType = 3;
    model.portrait = self.tenantProgressRecordInfoModel.portrait;
    chatVC.contactModel = model;
    chatVC.title = self.tenantProgressRecordInfoModel.name;
    [self.navigationController pushViewController:chatVC animated:YES];
}

#pragma mark - 加载数据
- (void)upDownLoadData {
    
    [self requestRentOutTakeLookRecordData];
}

#pragma mark -- 获取带看记录数据
- (void)requestRentOutTakeLookRecordData {
    
    WeakSelf
    [kNetworkManager requestTenantTakelookRecordWithArbpId:self.tenantProgressModel.arbpId success:^(NSArray<SISSTenantTakeLookModel *> *takeLookArr) {
            weakSelf.takeLookArr = takeLookArr;
    
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [weakSelf.tableView reloadData];
                [weakSelf.tableView.mj_header endRefreshing];
            }];
    
        } failed:^(NSError *error) {
    
            [weakSelf.tableView.mj_header endRefreshing];
            NSLog(@"%@",error);
        }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
